package com.yiren.manager.ctrl.lease;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrLeasePledge;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.lease.YrLeasePledgeForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.lease.YrLeasePledgeService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("leasePledge")
public class YrLeasePledgeController extends GenericController<YrLeasePledgeForm, YrLeasePledge>{

	@Autowired
	private YrLeasePledgeService selfService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrLeasePledgeForm> find(String searchVal) throws Exception{
		HTTPResult<YrLeasePledgeForm> result = new HTTPResult<YrLeasePledgeForm>();
		
		String sqlWhere = "elder_name=?1 OR elder_card_id=?2";
		QueryResult<YrLeasePledge> qr = selfService.find(YrLeasePledge.class, sqlWhere, new Object[]{searchVal,searchVal});
		List<YrLeasePledgeForm> formList = new ArrayList<YrLeasePledgeForm>();
		
		for(YrLeasePledge entity : qr.getResultList()){
			YrLeasePledgeForm form = toForm(entity);
			if(form.getHandleId()!=null){
				YrStaffInfo staffInfo = staffInfoService.findOne(form.getHandleId());
				form.setHandleName(staffInfo.getName());
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrLeasePledgeForm toForm(YrLeasePledge e) throws Exception {
		YrLeasePledgeForm form = ObjectUtils.objectToObject(e, YrLeasePledgeForm.class);
		return form;
	}

	@Override
	public YrLeasePledge toEntity(YrLeasePledgeForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
